var express = require("express");
var router = express.Router();
const Result = require("../utils/result");
const foodRecordService = require("../services/foodRecordService");

router.post("/", async function (req, res) {
  const { userId, recordDate } = req.body;
  const record = await foodRecordService.getRecord(userId, recordDate);
  res.json(Result.success(record));
});

router.post("/add", async function (req, res) {
  const foodRecordArr = req.body.foodRecord;
  await foodRecordArr.forEach((item) => {
    foodRecordService.add(item);
  });
  res.json(Result.success());
});

router.get("/remove", async function (req, res) {
  const { id } = req.query;
  await foodRecordService.remove(id);
  res.json(Result.success());
});

router.post("/update", async function (req, res) {
  const foodRecord = req.body.foodRecord;
  await foodRecordService.update(foodRecord);
  res.json(Result.success());
});

module.exports = router;
